package chances.iptv.web;

/*
 * Copyright (c) 2010 by Chances.
 * $HeadURL$:
 * $Author: zhanghui $:
 * $Date: 2011/11/17 06:23:45 $:
 * $Rev$:
 */

import java.text.MessageFormat;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

/**
 * 产品跳转控制类
 * 
 * @author <a href="mailto:chenxx@chances.com.cn">chenxx</a>
 */
public class ProductDispatcherController extends AbstractController {

    private static final Log sysLog = LogFactory
            .getLog(ProductDispatcherController.class);

    private static final Log log = LogFactory.getLog("productPVLog");

    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest req,
            HttpServletResponse resp) throws Exception {
        String ps = req.getParameter("ps");
        String p = req.getParameter("p");
        String productId = req.getParameter("productId");
        String name = req.getParameter("name");
        String url = req.getParameter("url");
        
        String returnURL = req.getParameter("returnURL");
        String backURL = req.getParameter("backURL");

        try {
            UserSession userSession = UserSession.getUserSession(req);
            String userAgent = req.getHeader("User-agent");
            String logItem[] = new String[] { userSession.getUserId(), ps,
                    productId,
                    new String(name.getBytes("ISO_8859_1"), "UTF-8"), p, url,
                    userAgent };
            java.text.MessageFormat message = new MessageFormat(
                    "{0},{1},{2},{3},{4},{5}");
            String logs = message.format(logItem);
            log.info(logs);

        } catch (java.lang.Throwable e) {
            sysLog.error(e.getMessage(), e);
        }
        req.setAttribute("url", url);
        req.setAttribute("returnURL", returnURL);
        req.setAttribute("backURL", backURL);
        // if(null != url) {
        // resp.sendRedirect(url);
        // }
        return new ModelAndView("/productDispatch");
    }
}
